package groep3.ssc.controllers;

import groep3.ssc.model.Race;
import groep3.ssc.skaterViews.SkaterViewTemplate;

import java.util.ArrayList;

public class ViewController
{

    private ArrayList<SkaterViewTemplate> skaterViews;
    private static ViewController instance = null;

    public static ViewController getInstance()
    {
        if (instance == null)
        {
            instance = new ViewController();
        }
        return instance;
    }

    private ViewController()
    {
        skaterViews = new ArrayList<SkaterViewTemplate>();
    }

    public void update()
    {

        for (final SkaterViewTemplate view : skaterViews)
        {
            updateView(view);
        }
    }

    public void updateView(final SkaterViewTemplate view)
    {
        final Race model = ModelRepository.getInstance().getActiveRace();
        if (model == null || model.getOurSkater() <= 0)
            return;

        view.post(new Runnable()
        {

            @Override
            public void run()
            {
                view.update(model);
            }
        });
    }

    public void addSkaterView(SkaterViewTemplate view)
    {
        this.skaterViews.add(view);
        updateView(view);
    }

    public void removeSkaterView(SkaterViewTemplate view)
    {
        this.skaterViews.remove(view);
    }
}